//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.mega.ycy.mixin;

import com.mega.ycy.Saver;
import com.mega.ycy.Yiciyuan_EVENT;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiIngame;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.profiler.Profiler;
import net.minecraftforge.client.GuiIngameForge;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.lwjgl.Sys;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.Mixins;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@IFMLLoadingPlugin.SortingIndex(Integer.MIN_VALUE)
public class TWEAKER implements IFMLLoadingPlugin {
    public static boolean classesLoaded = false;
    public static boolean isTimeStop = false;
    public static int sysytemTime = 0;
    public static Saver<RenderManager> RENDER_MANAGER;
    public static Saver<EntityRenderer> ENTITY_RENDERER;
    public static Saver<RenderGlobal> RENDER_GLOBAL;
    public static Saver<Profiler> CLIENT_PROFILER;
    public static Saver<GuiIngameForge> INGAME_GUI;
    public static Timer NORMAL = new Timer(20.0F);
    public static void in() {
        MixinBootstrap.init();
        Mixins.addConfiguration("mixins.ycy.json");
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleAtFixedRate(() ->{
            sysytemTime++;
            if (sysytemTime > 900000000) {
                classesLoaded = true;
                scheduledExecutorService.shutdown();
            }
        }, 1 ,1, TimeUnit.NANOSECONDS);
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(NORMAL::updateTimer, 10, 10, TimeUnit.MICROSECONDS);
    }



    public void init() {
        try {
            ClassLoader appClassLoader = Launch.class.getClassLoader();
            //appClassLoader.loadClass("com.realTerminal.realTerminalMod.entity.RealTerminalEntity");
            MethodUtils.invokeMethod(appClassLoader, true, "addURL", this.getClass().getProtectionDomain().getCodeSource().getLocation());
            MethodUtils.invokeStaticMethod(appClassLoader.loadClass(this.getClass().getName()), "in");

        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public static void reloadFields() {
        Minecraft mc = Minecraft.getMinecraft();
        if (mc.renderGlobal != null) {
            if (mc.renderGlobal.getClass().getName().equals(RenderGlobal.class.getName()))
                RENDER_GLOBAL = new Saver<>(mc.renderGlobal);
            else {
                mc.renderGlobal = RENDER_GLOBAL.object();
            }
        }
        if (mc.mcProfiler != null) {
            if (mc.mcProfiler.getClass().getName().equals(Profiler.class.getName()))
                CLIENT_PROFILER = new Saver<>(mc.mcProfiler);
            else {
                mc.mcProfiler = CLIENT_PROFILER.object();
            }
        }
        if (mc.entityRenderer != null) {
            if (mc.entityRenderer.getClass().getName().equals(EntityRenderer.class.getName()))
                ENTITY_RENDERER = new Saver<>(mc.entityRenderer);
            else {
                mc.entityRenderer = ENTITY_RENDERER.object();
            }
        }
        if (mc.renderManager != null) {
            if (mc.renderManager.getClass().getName().equals(RenderManager.class.getName()))
                RENDER_MANAGER = new Saver<>(mc.renderManager);
            else {
                mc.renderManager = RENDER_MANAGER.object();
            }
        }
    }

    public TWEAKER() {
        this.init();
    }

    public String[] getASMTransformerClass() {
        return new String[0];
    }

    public String getModContainerClass() {
        return null;
    }

    @Nullable
    public String getSetupClass() {
        return null;
    }

    public void injectData(Map<String, Object> data) {
    }


    public String getAccessTransformerClass() {
        return null;
    }

    @SideOnly(Side.CLIENT)
    public static class Timer
    {

        public int elapsedTicks;
        public float renderPartialTicks;
        public float elapsedPartialTicks;
        public long lastSyncSysClock;
        public float tickLength;

        public static long getSystemTime()
        {
            return Sys.getTime() * 1000L / Sys.getTimerResolution();
        }

        public Timer(float tps)
        {
            this.tickLength = 1000.0F / tps;
            this.lastSyncSysClock = getSystemTime();
        }

        public void updateTimer()
        {
            long i = getSystemTime();
            this.elapsedPartialTicks = (float)(i - this.lastSyncSysClock) / this.tickLength;
            this.lastSyncSysClock = i;
            this.renderPartialTicks += this.elapsedPartialTicks;
            this.elapsedTicks = (int)this.renderPartialTicks;
            this.renderPartialTicks -= (float)this.elapsedTicks;
            reloadFields();
        }
    }
}
